package com.demo.javaDemo.concurrent;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.function.Predicate;

import com.demo.javaDemo.util.DateTimeUtil;

@SuppressWarnings("serial")
public class FileFinder extends SimpleFileVisitor<Path>{
	private final static ForkJoinPool forkJoinPool = new ForkJoinPool();

	final Predicate<Path> test;

	public FileFinder( Predicate<Path> test) {
		this.test = test;
	}



	@Override
	public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
		if(dir.startsWith(".")) {
			return FileVisitResult.SKIP_SUBTREE;
		}
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
		if(test.test(file)) {
			System.out.println(">>>>>>>"+file.getFileName());
			System.out.println(">>>>>>>"+file.toString());
			System.out.println(">>>>>>>"+file.getNameCount());
			System.out.println(DateTimeUtil.formatDateTime(Date.from(attrs.creationTime().toInstant())));
			System.out.println(DateTimeUtil.formatDateTime(Date.from(attrs.lastModifiedTime().toInstant()))); 
			System.out.println(DateTimeUtil.formatDateTime(Date.from(attrs.lastAccessTime().toInstant()))); 
			LocalDateTime.now().atZone(ZoneId.systemDefault());
			System.out.println(attrs.size());
			return FileVisitResult.TERMINATE;
		}
		
		return FileVisitResult.CONTINUE;
	}



	public static void main(final String[] args) {
		System.out.println();
		final long start = System.nanoTime();
		FileFinder.find("I:\\gitWorkspace", ff -> ff.getFileName().toString().endsWith(".java"));
		System.out.println("Time taken: " + (System.nanoTime() - start) / 1.0e6);
	}

	public static void find(String dir, Predicate<Path> test) {
		try {
			Path pp=Files.walkFileTree(Paths.get(dir), new FileFinder( test));
			System.out.println(">>>>>>>>>>>>>"+pp);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}